package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;


/**
 * Servlet implementation class ServeltContextForServlet
 */
@WebServlet(name="myServeltContextForServlet",value="/servlet/ServeltContextForServlet")
public class ServeltContextForServlet extends HttpServlet {
	private Logger logger = Logger.getLogger(ServeltContextForServlet.class);
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		logger.debug("ServeltContextForServlet>>doGet  start……");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		ServletContext servletContext = this.getServletContext();
		//获取标志
		String flag = request.getParameter("flag");
		if(flag.equals("add")) {
			servletContext.setAttribute("key01", "value01");
		}else if(flag.equals("delete")) {
			servletContext.removeAttribute("key01");
		}else if(flag.equals("replace")) {
			servletContext.setAttribute("key01", "value02");
		}
		response.sendRedirect(""+request.getContextPath()+"/html01_result.html");
		logger.debug("ServeltContextForServlet>>doGet  end……");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		logger.debug("ServeltContextForServlet>>doPost  start……");
		this.doGet(request, response);
		logger.debug("ServeltContextForServlet>>doPost  end……");
	}

}
